/*
 * Create:      柿子
 * CreateDate:  2023/11/16
 * Describe：   复制病区信息配置【复制配置】
 * */
import React, { Component } from 'react';
import { Modal, Select, Button } from 'antd';
import { Util } from 'tools';
import store from 'store';
import PublicTablePagination from 'pages/common/PubilcTablePagination.jsx';

class CopyWardInfoConfig extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      rowData: {},
      loading: false,
      tableData: [],
      columns: [],
      totalWidth: 0,
      selectData: {},
      queryParams: {}
    }
  };

  static getDerivedStateFromProps(nextProps, prevState) {
    if (JSON.stringify(nextProps.selectData) !== JSON.stringify(prevState.selectData)) {
      return {
        selectData: nextProps.selectData
      }
    }
    return null
  }

  componentDidMount() {
    this.props.onRef && this.props.onRef(this);
  };

  // 修改弹窗状态
  modifyVisible = (visible) => {
    let { columns } = this.state;
    this.setState({ visible }, () => {
      this.state.visible && this.getTableData();
      if (!(columns && columns.length > 0) && this.state.visible) {
        this.getTableColumns();
      }
    })
  };

  // 获取列表表头数据
  getTableColumns = async () => {
    try {
      let data = {
        params: [{
          compontName: 'CopyWardInfoConfig',
          type: 'C',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      const columns = res.result.C || [];
      this.setState({ columns, totalWidth: res.totalWidth });
    } catch (error) {
      console.log(error);
    }
  };

  // 获取列表数据
  getTableData = async () => {
    try {
      const { queryParams } = this.state;
      let data = {
        params: [{
          ...queryParams
        }]
      }
      let res = await React.$asyncPost(this, '123', data);
      let tableData = Util.addKeyValueToDataSource(res?.result?.rows || []);
      this.setState({ tableData });
    } catch (error) {
      console.log(error);
    }
  };

  // 操作框change事件
  handleInputChange = (e, dataIndex) => {
    let { queryParams } = this.state;
    queryParams[dataIndex] = e;
    this.setState({});
  };

  handleOk = () => {

  };

  render() {
    const { visible, loading, tableData, totalWidth, columns, queryParams, selectData } = this.state;
    return (
      <Modal
        title="复制配置"
        width="50vw"
        visible={visible}
        onCancel={() => this.modifyVisible(false)}
        onOk={this.handleOk}
      >
        <div className="table-body-height">
          <div style={{ marginBottom: 12 }}>
            医院：
            <Select
              allowClear
              showSearch
              placeholder="请选择"
              optionFilterProp="search"
              className="common-query-input"
              value={queryParams?.hospID}
              onChange={(e) => this.handleInputChange(e, 'hospID')}
            >
              {React.$SelectOptions(selectData?.hosp || [])}
            </Select>
            科室：
            <Select
              allowClear
              showSearch
              placeholder="请选择"
              optionFilterProp="search"
              className="common-query-input"
              value={queryParams?.locID}
              onChange={(e) => this.handleInputChange(e, 'locID')}
            >
              {React.$SelectOptions(selectData?.loc || [])}
            </Select>
            <Button type="primary" onClick={this.getTableData}>查询</Button>
          </div>
          <PublicTablePagination
            param={{
              loading,
              columns, // 表头配置
              data: tableData, // 表格数据
              x: totalWidth, // 表格的宽度
              y: store.getState().tableHeight.y - 150,
              height: store.getState().tableHeight.y - 100,
              componentName: 'CopyWardInfoConfig',
            }}
            getColumns={this.getTableColumns}
          />
        </div>
      </Modal>
    )
  }
};

export default CopyWardInfoConfig;